package cn.e4j.modular.api.controller;

import cn.e4j.core.dto.impl.Dto;
import cn.e4j.core.dto.requestDtoResolver.DtoParam;
import cn.e4j.core.dto.requestDtoResolver.DtoWapper;
import cn.e4j.core.reqres.response.ResponseData;
import cn.e4j.core.util.DateUtils;
import cn.e4j.core.util.ToolUtil;
import cn.e4j.modular.api.service.Gwat151Service;
import cn.e4j.modular.api.util.CommonUtils;
import cn.e4j.modular.api.util.ControllerSupport;
import cn.e4j.modular.crm.entity.GwbaT;
import cn.e4j.modular.system.entity.SysUser;
import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.math.BigDecimal;
import java.time.LocalDate;
import java.util.Date;
import java.util.List;

/**
 * Created by yuhuan on 2018/07/10.
 */
@RestController
@RequestMapping("api/wx/gwat151")
public class Gwat151Controller extends ControllerSupport {

    @Autowired
    private Gwat151Service gwat151Service;

    /**
     * 获取日历信息
     *
     * @return
     */
    @RequestMapping(value = "getGwba")
    public ResponseData getGwba(@DtoParam DtoWapper wapper) {
        Dto inDto = wapper.getDto();
        SysUser currentUser = wapper.getSysUser();
        inDto.put("GWBAID", currentUser.getID());
        List<Dto> list = gwat151Service.getGwba(inDto);
        return SUCCESS(list);
    }

    /**
     * 获取日历信息
     *
     * @return
     */
    @RequestMapping(value = "getAllTask")
    public ResponseData getAllTask(@DtoParam DtoWapper wapper) {
        Dto inDto = wapper.getDto();
        SysUser currentUser = wapper.getSysUser();
        inDto.put("GWBAID", currentUser.getID());
        List<Dto> list = gwat151Service.getAllTask(inDto);
        return SUCCESS(list);
    }

    /**
     * 获取客户信息
     *
     * @return
     */
    @RequestMapping(value = "getGwakPage")
    public ResponseData getGwakPage(@DtoParam DtoWapper wapper) {
        Dto inDto = wapper.getDto();
        SysUser currentUser = wapper.getSysUser();
        inDto.put("GWAK019", currentUser.getACCOUNT());
        List<Dto> list = gwat151Service.getGwakPage(wapper.getPage(), inDto);
        return SUCCESS(list);
    }

    /**
     * 保存
     *
     * @return
     */
    @RequestMapping(value = "gwat151Save")
    public ResponseData orderSave(@DtoParam DtoWapper wapper) {
        Dto inDto = wapper.getDto();
        SysUser currentUser = wapper.getSysUser();
        Date date = new Date();
        GwbaT gwba_tPO = JSON.parseObject(JSON.toJSONString(wapper.getDto()), GwbaT.class);
        try {
            gwba_tPO.setGWBAENT(currentUser.getDBC());
            gwba_tPO.setGWBAID(currentUser.getID());

            // 项次+1
            inDto.put("GWBAENT", inDto.getString("ENT"));
            inDto.put("GWBAACTI", "Y");
            inDto.put("GWBAID", currentUser.getID());
            List<Dto> maxlist = gwat151Service.getTaskMax(inDto);
            if (maxlist.size() > 0 && ToolUtil.isNotEmpty(maxlist.get(0))) {
                Dto maxDto = maxlist.get(0);
                gwba_tPO.setGWBASEQ(maxDto.getBigDecimal("GWBASEQ").add(BigDecimal.ONE));
            } else {
                gwba_tPO.setGWBASEQ(BigDecimal.ONE);
            }

            gwba_tPO.setGWBA007(inDto.getString("GWBA007"));
            gwba_tPO.setGWBA001(inDto.getString("GWBA001"));//事项
            gwba_tPO.setGWBA005(inDto.getString("GWBA005"));
            gwba_tPO.setGWBA002(inDto.getString("GWBA002"));
            gwba_tPO.setGWBADOCDATE(inDto.getLocalDate("GWBADOCDATE"));
            //gwba_tPO.setGWBADOCDATE(CommonUtils.dateToLocalDate(inDto.getDate("GWBADOCDATE")));
            gwba_tPO.setGWBA003(inDto.getString("GWBA003").replace("T", " ").substring(0, 16));//开始时间
            gwba_tPO.setGWBA004(inDto.getString("GWBA004").replace("T", " ").substring(0, 16));//结束时间
            gwba_tPO.setGWBA009(inDto.getString("GWBA009"));
            gwba_tPO.setGWBA010(inDto.getString("GWBA010"));
            gwba_tPO.setGWBA011(inDto.getString("GWBA011"));
            gwba_tPO.setGWBA012(inDto.getBigDecimal("GWBA012"));
            gwba_tPO.setGWBA014(inDto.getString("GWBA014"));
            gwba_tPO.setGWBA006(inDto.getString("GWBA006"));

            gwba_tPO.setGWBA015(inDto.getString("GWBA015"));//拜访客户编号
            gwba_tPO.setGWBA016(inDto.getString("GWBA016"));//拜访人员姓名
            gwba_tPO.setGWBA017(inDto.getString("GWBA017"));//职位编号
            gwba_tPO.setGWBA018(inDto.getString("GWBA018"));//角色编号


            gwba_tPO.setGWBAACTI("Y");
            gwba_tPO.setGWBAUSER(currentUser.getID());
            gwba_tPO.setGWBAGRUP(currentUser.getORG_ID());
            gwba_tPO.setGWBAORIU(currentUser.getID());
            gwba_tPO.setGWBAORIG(currentUser.getORG_ID());
            gwba_tPO.setGWBADATE(LocalDate.now());
            gwba_tPO.setGWBATIME(DateUtils.formatDate(date, DateUtils.PATTERN_HH_mm_ss));

            gwat151Service.gwat151Save(gwba_tPO);

        } catch (Exception e) {
            e.printStackTrace();
            return ERROR;
        }

        return SUCCESS;
    }

    /**
     * 修改
     *
     * @return
     */
    @RequestMapping(value = "gwat151Update")
    public ResponseData orderUpdate(@DtoParam DtoWapper wapper) {
        Dto indto = wapper.getDto();
        GwbaT gwba_tPO = new GwbaT();

        SysUser currentUser = wapper.getSysUser();
        Date date = new Date();
        try {

            indto.put("GWBA003", indto.getString("GWBA003").replace("T", " ").substring(0, 16));//开始时间
            indto.put("GWBA004",indto.getString("GWBA004").replace("T", " ").substring(0, 16));//结束时间

            gwba_tPO = CommonUtils.toJavaClass(indto, GwbaT.class);
            gwba_tPO.setGWBAMODU(currentUser.getID());
            gwba_tPO.setGWBATIME(DateUtils.formatDate(date, DateUtils.PATTERN_HH_mm_ss));
            //WHERE  gwbaent = #{gwbaent} AND gwbaid = #{gwbaid} AND gwbaseq = #{gwbaseq}
            UpdateWrapper<GwbaT> wrapper = new UpdateWrapper<>();
            wrapper.eq("GWBAENT", gwba_tPO.getGWBAENT());
            wrapper.eq("GWBAID", gwba_tPO.getGWBAID());
            wrapper.eq("GWBASEQ", gwba_tPO.getGWBASEQ());
            gwba_tPO.update(wrapper);

        } catch (Exception e) {
            e.printStackTrace();
            return ERROR(500, "服务器出小差了");
        }

        return SUCCESS;
    }


    /**
     * 删除信息
     *
     * @return
     */
    @RequestMapping("gwat151Delete")
    public ResponseData orderDelete(@DtoParam DtoWapper wapper) {
        Dto inDto = wapper.getDto();
        try {
            gwat151Service.gwat151Delete(inDto);
        } catch (Exception e) {
            e.printStackTrace();
            return ERROR;
        }
        return SUCCESS;
    }
}
